package com.jbh.recordlibrary.u;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.core.app.ActivityCompat;

import java.util.ArrayList;
import java.util.List;

public class PermissionUtil {

    public static final int CHECK_PERMISSION_REQUEST_CODE =  1000 ;


    /**
     * @param activity
     * @param permissions
     * @param checkPermissionListener
     */
    public static void  checkPermission(Activity activity, List<String> permissions, CheckPermissionListener checkPermissionListener){
        if (permissions!=null && permissions.size()>0 && Build.VERSION.SDK_INT>= Build.VERSION_CODES.M){
            List<String> list = new ArrayList<>() ;
            for (String p:permissions){
                if (ActivityCompat.checkSelfPermission(activity,p) !=  PackageManager.PERMISSION_GRANTED){
                    list.add(p);
                }
            }
            if (list.size()>0){
                checkPermissionListener.startRequestPermission();
                ActivityCompat.requestPermissions(activity, list.toArray(new String[list.size()]),CHECK_PERMISSION_REQUEST_CODE);
            }else {
                checkPermissionListener.noNeedRequestPermission();
            }
        }else {
            checkPermissionListener.noNeedRequestPermission();
        }
    }




    public interface CheckPermissionListener{
        void startRequestPermission();
        void noNeedRequestPermission();
    }
}
